package com.springdemo.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.springdemo.entity.UserEntity;
import com.springdemo.user.service.UserService;
import com.springdemo.util.CopyEntityToVo;
import com.springdemo.vo.UserVo;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;
	
	@GetMapping("/userInfo/{userId}")
	@ResponseBody
	public UserVo findUserInfoById(@PathVariable String userId){
		UserEntity user=userService.findUserById(userId);
		UserVo result = new UserVo();
		CopyEntityToVo.copyPropeties(user, result);
		return result;
	}
	@PutMapping("userInfo/save")
	@ResponseBody
	public int saveUserInfo(UserVo user){
		UserEntity saveUser = new UserEntity();
		CopyEntityToVo.copyPropeties(user, saveUser);
		int result = userService.insert(saveUser);
		return result;
	}
	
	@PutMapping("userInfo/update")
	@ResponseBody
	public int updateUserInfo(UserVo user){
		UserEntity saveUser = new UserEntity();
		CopyEntityToVo.copyPropeties(user, saveUser);
		int result = userService.updateById(saveUser);
		return result;
	}
	
}
